{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List Comprehension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-04T07:20:18.948695Z",
     "start_time": "2018-09-04T07:20:18.942151Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25]\n"
     ]
    }
   ],
   "source": [
    "# List containing the square of first 5 numbers\n",
    "\n",
    "lst = []\n",
    "\n",
    "for i in range(1, 6):\n",
    "    lst.append(i**2)\n",
    "    \n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T06:35:55.020560Z",
     "start_time": "2018-09-05T06:35:55.015603Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25]\n"
     ]
    }
   ],
   "source": [
    "# Lets try the above code using list comprehension\n",
    "\n",
    "lst = [ i**2 for i in range(1, 6) ]\n",
    "\n",
    "print(lst)\n",
    "\n",
    "# SYNTAX : lst = [ expression for i in sequence ]\n",
    "# i.e. for every 'i' in the 'sequence' apply the 'expression'\n",
    "# and add that 'expression' value to the lst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T06:36:41.369587Z",
     "start_time": "2018-09-05T06:36:41.364604Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['P', 'Y', 'T', 'H', 'O', 'N']\n"
     ]
    }
   ],
   "source": [
    "lst = [ w for w in 'PYTHON' ]\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Very easy and elegant.\n",
    "\n",
    "# Length of the code is going to reduce.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Lets now try to apply the condition to 'i'\n",
    "# lst = [ expression for i in sequence if condition ]\n",
    "# i.e. for every 'i' in the 'sequence' if that 'i' \n",
    "# satisfies the condition, than apply the 'expression'\n",
    "# and add that 'expression' value to the lst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T06:37:08.642787Z",
     "start_time": "2018-09-05T06:37:08.631810Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841]\n"
     ]
    }
   ],
   "source": [
    "lst = [ i**2 for i in range(1, 30) ]\n",
    "\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T02:18:33.471906Z",
     "start_time": "2018-09-05T02:18:33.466858Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784]\n"
     ]
    }
   ],
   "source": [
    "lst = [ i**2 for i in range(1, 30) if i%2==0 ]\n",
    "\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T04:32:50.499758Z",
     "start_time": "2018-09-05T04:32:50.490782Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['S', 'K', 'F', 'B']\n"
     ]
    }
   ],
   "source": [
    "sup_hero = ['Shaktiman', 'Krish', 'Flying Jatt', 'Bhavesh Joshi']\n",
    "\n",
    "# Try to extract the first Character of each name into a list.\n",
    "\n",
    "lst = [ word[0] for word in sup_hero]\n",
    "\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T04:43:59.936149Z",
     "start_time": "2018-09-05T04:43:59.924191Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['THE', 3], ['QUICK', 5], ['BROWN', 5], ['FOX', 3], ['JUMPS', 5], ['OVER', 4], ['THE', 3], ['LAZY', 4], ['DOG', 3]]\n"
     ]
    }
   ],
   "source": [
    "s = 'the quick brown fox jumps over the lazy dog'\n",
    "\n",
    "# Convert each word into upper case and \n",
    "# count the number of characters in each word\n",
    "\n",
    "lst = [ [word.upper(), len(word)] for word in s.split() ]\n",
    "\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Anonymous Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T02:30:36.969358Z",
     "start_time": "2018-09-05T02:30:36.963257Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "def square(n):\n",
    "    return n*n\n",
    "\n",
    "print(square(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T02:31:26.094310Z",
     "start_time": "2018-09-05T02:31:26.088243Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "lam_sq = lambda n : n*n\n",
    "\n",
    "print(lam_sq(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T05:09:47.238926Z",
     "start_time": "2018-09-05T05:09:47.233939Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "# Product of two numbers\n",
    "\n",
    "prod = lambda a, b : a*b\n",
    "\n",
    "print(prod(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T05:16:18.198036Z",
     "start_time": "2018-09-05T05:16:18.192034Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "# Larger of two numbers\n",
    "\n",
    "# a>b?a:b\n",
    "# a if a>b else b\n",
    "\n",
    "greater = lambda a,b : a if a>b else b\n",
    "\n",
    "print(greater(2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T02:36:09.532405Z",
     "start_time": "2018-09-05T02:36:09.527419Z"
    }
   },
   "outputs": [],
   "source": [
    "# It is possible to pass a function as an argument to another function\n",
    "# In this case we should go for LAMBDA functions\n",
    "\n",
    "# If we have only one time requirement, than why to go for seperate top level functions?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### filter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T02:37:30.282860Z",
     "start_time": "2018-09-05T02:37:30.277926Z"
    }
   },
   "outputs": [],
   "source": [
    "# Suppose we have 10 numbers, and we want to filter only \n",
    "# even numbers from that.\n",
    "\n",
    "# So based on some contition if we want to filter a particular group."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T02:40:38.778145Z",
     "start_time": "2018-09-05T02:40:38.773158Z"
    }
   },
   "outputs": [],
   "source": [
    "# Syntax : filter(function, sequence)\n",
    "\n",
    "# function always returns True or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T07:01:52.171578Z",
     "start_time": "2018-09-05T07:01:52.166584Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "def isEven(x):\n",
    "    if x%2==0:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    \n",
    "\n",
    "lst = [0,1,2,3,4,5,6,7,8,9,10]\n",
    "\n",
    "f = list(filter(isEven, lst))\n",
    "\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T05:23:19.068826Z",
     "start_time": "2018-09-05T05:23:19.061845Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "lst = [0,1,2,3,4,5,6,7,8,9,10]\n",
    "\n",
    "f = list(filter(lambda x : x%2==0, lst))\n",
    "\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T03:24:26.796859Z",
     "start_time": "2018-09-05T03:24:26.792870Z"
    }
   },
   "outputs": [],
   "source": [
    "# If the requirement is to map each value of sequence to something\n",
    "\n",
    "# Lets say, we want to find the double of each element in a sequence\n",
    "\n",
    "# Syntax : map(function, sequence)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T03:25:19.203171Z",
     "start_time": "2018-09-05T03:25:19.197186Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "def double(x):\n",
    "    return 2*x\n",
    "\n",
    "lst = [1, 2, 3, 4, 5]\n",
    "d = list(map(double, lst))\n",
    "\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T03:25:57.824171Z",
     "start_time": "2018-09-05T03:25:57.818174Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 14, 16, 18, 20]\n"
     ]
    }
   ],
   "source": [
    "lst = [6, 7, 8, 9, 10]\n",
    "d = list(map(lambda x : 2*x, lst))\n",
    "\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T07:10:34.704475Z",
     "start_time": "2018-09-05T07:10:34.698491Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 10, 18]\n"
     ]
    }
   ],
   "source": [
    "# multiply two lists element wise\n",
    "\n",
    "lst_1 = [1, 2, 3, 10]\n",
    "lst_2 = [4, 5, 6]\n",
    "\n",
    "lst_3 = list(map(lambda x, y : x*y, lst_1, lst_2))\n",
    "\n",
    "print(lst_3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### reduce()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T03:33:41.123164Z",
     "start_time": "2018-09-05T03:33:41.119176Z"
    }
   },
   "outputs": [],
   "source": [
    "# reduce() function reduces sequence of elements into a single \n",
    "# element by applying the specified function.\n",
    "\n",
    "# reduce() function present in functools module\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T03:35:22.280453Z",
     "start_time": "2018-09-05T03:35:22.274759Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28\n"
     ]
    }
   ],
   "source": [
    "from functools import *\n",
    "\n",
    "lst = [1, 2, 3, 4, 5, 6, 7]\n",
    "\n",
    "r = reduce(lambda x,y : x+y, lst)\n",
    "\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Function Aliasing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T07:14:47.474688Z",
     "start_time": "2018-09-05T07:14:47.467728Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n",
      "36\n"
     ]
    }
   ],
   "source": [
    "def sq(n):\n",
    "    return n*n\n",
    "\n",
    "new_sq = sq\n",
    "\n",
    "print(new_sq(5))\n",
    "\n",
    "print(sq(6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-05T07:17:58.965782Z",
     "start_time": "2018-09-05T07:17:58.961789Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!!\n"
     ]
    }
   ],
   "source": [
    "def fun():\n",
    "    return 'Hello World!!'\n",
    "\n",
    "# What is the difference between following lines?\n",
    "\n",
    "# f1 = fun\n",
    "f1 = fun()\n",
    "print(f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
